﻿using System;
using System.Diagnostics;
using System.Windows.Input;

namespace BlogApp.WP8.Utils
{
    public class NavigateCommand : ICommand
    {
        public bool CanExecute(object parameter)
        {
            return true;
        }

        //See http://blogs.msdn.com/b/trevor/archive/2008/08/14/c-warning-cs0067-the-event-event-is-never-used.aspx
        public event EventHandler CanExecuteChanged
        {
            add { }
            remove { }
        }

        public void Execute(object uri)
        {
            if (uri is String)
            {
                var root = App.RootFrame;
                root.Navigate(new Uri(uri as String, UriKind.RelativeOrAbsolute));
            }
        }

        public void ClearBackStack()
        {
            var root = App.RootFrame;
            try
            {
                while (root.RemoveBackEntry() != null)
                {
                    //do nothing
                }
            }
            catch (Exception e)
            {
                Debug.WriteLine("backstack cleared");
            }
        } 
    }
}